package com.runfast.waimai.web.controller.Driver_controller;

import com.runfast.paotui.dao.model.RunfastLoginRecord;
import com.runfast.paotui.dao.model.RunfastShopper;
import com.runfast.paotui.service.RunfastShopperService;
import com.runfast.waimai.dao.model.RunfastAccountrecord;

import com.runfast.waimai.dao.model.RunfastWithdraw;
import com.runfast.waimai.service.RunfastAccountrecordService;
import com.runfast.waimai.service.RunfastShoppingtrolleyService;
import com.runfast.waimai.service.RunfastWithdrawService;
import com.runfast.waimai.util.AESUtil;
import com.runfast.waimai.util.PageBean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.Date;
import java.util.List;


@Controller
@RequestMapping("/driver/balance/*")
public class DriverBalanceController extends DriverBaseController {
    //骑手余额 提现 收入明细页面
	 @Resource
	 private RunfastShopperService shopperService;
	 @Resource
	 private RunfastAccountrecordService accountRecordService;
	//账号变动清单
	 @Resource
	 private RunfastWithdrawService withdrawService;
	//  runfast_withdraw  提现申请
	//余额主页
			@PostMapping(value="index")//验证
			public @ResponseBody ModelMap index(String token ){  //骑手id
		//		String a="abcdefghijklmn";
				ModelMap model = new ModelMap();
		//	Integer id=Integer.parseInt(AESUtil.decrypt(token,a));
				RunfastShopper shopper  = super.getLoginShopper(token);
				if(shopper!=null){
					 model.put("shopper",shopperService.get(shopper.getId()));//通过登陆Id获得骑手余额信息
				}
				return model;
			}

	//获取提现记录
		@RequestMapping(value="incomelist",method=RequestMethod.POST)//验证
		public @ResponseBody ModelMap incomelist(@RequestParam("page") int page,@RequestParam(value="rows") int rows,String token){
			ModelMap model = new ModelMap();
			RunfastShopper shopper  = super.getLoginShopper(token);//获得骑手的信息
			if(shopper!=null){
			RunfastAccountrecord accountRecord   =	new RunfastAccountrecord();//账号变动清单
			accountRecord.setType(0);////0:消费,1:充值,3:退款,4提现
			accountRecord.setGenretype(1);//// 0用户 1骑手
			accountRecord.setCid(shopper.getId());//用户ID

			List<RunfastAccountrecord> a= accountRecordService.findPageBean(page, rows,accountRecord);//总页数type ,genretype cid
				int count=accountRecordService.findCount(page, rows,accountRecord);//只查总次数type ,genretype cid
				PageBean<RunfastAccountrecord>pageBean=new PageBean<RunfastAccountrecord>() ;
				pageBean.setData(a);//设置当页面数据
				pageBean.setCurrentPage(rows);// 当前页应该是从前端传过来的才对，这个rows 不对
				pageBean.setTotalPage(count);
				model.put("accountRecord", pageBean.getData());// // 当前页数据
				model.put("currentPage", pageBean.getCurrentPage()); // 当前页应该是从前端传过来的才对
				model.put("totalPage", pageBean.getTotalPage());//总页数
					//假如总页数 10  每页面显示2  limit 0,2 当前页面为1
			/*PageBean<RunfastAccountrecord> pageBean  = accountRecordService.getPageBeanbydriver(page, rows, accountRecord);
			model.put("accountRecord", pageBean.getData());// // 当前页数据
			model.put("currentPage", pageBean.getCurrentPage()); // 当前页
			model.put("totalPage", pageBean.getTotalPage());//总页数*/
			}
			return model;
		}

	//到提现页面
		@RequestMapping(value="getreach",method=RequestMethod.POST)
		public @ResponseBody ModelMap getreach(String token){
			ModelMap model = new ModelMap();
			RunfastShopper shopper = super.getLoginShopper(token);
			if(shopper!=null){
		    model.put("shopper",shopperService.get(shopper.getId()));
			}
		     	return model;
			}
	//获取提现记录
		@RequestMapping(value="getprice",method=RequestMethod.POST)
		public @ResponseBody ModelMap getprice(RunfastWithdraw t,String token){// //  runfast_withdraw  提现申请
			ModelMap model = new ModelMap();
			RunfastShopper shopper = super.getLoginShopper(token);
			if(shopper!=null){
		    RunfastShopper shop = shopperService.get(shopper.getId());
		    if(shop.getRemainder().compareTo(shop.getRemainder())>=0){//Remainder余额  compareTo和撒。。比较
			t.setUserid(shopper.getId());//从骑手端的ID得到提现用户ID
			t.setUsername(shop.getName());//设置用户名
			t.setUsermobile(shop.getMunber());//设置用户的手机号
			t.setStatus(0);//// 状态0待审核,1通过，2不通过
			t.setApplytype(2);//  申请人 1用户 2骑手 3商家
			t.setCreatetime(new Date());//设置系统时间
			withdrawService.insert(t);
			model.put("succ","申请成功");
		    }else{
		    	model.put("flo","余额不足！");
		    }
		}
		     	return model;
			}
	//收入明细
		@RequestMapping(value="incomeDetail",method=RequestMethod.POST)
		public @ResponseBody ModelMap incomeDetail(RunfastWithdraw t, @RequestParam("page") int page, @RequestParam(value="rows") int rows,String token){
			ModelMap model = new ModelMap();
			RunfastShopper shopper  = super.getLoginShopper(token);
			if(shopper!=null){
			RunfastAccountrecord accountRecord   =	new RunfastAccountrecord();//账号变动清单
			accountRecord.setGenretype(1);// 0用户 1骑手
			accountRecord.setCid(shopper.getId());//用户ID
				List<RunfastAccountrecord> a= accountRecordService.findPageBean(page, rows,accountRecord);//总页数type ,genretype cid
				int count=accountRecordService.findCount(page, rows,accountRecord);//只查总次数type ,genretype cid
				PageBean<RunfastAccountrecord>pageBean=new PageBean<RunfastAccountrecord>() ;
				pageBean.setData(a);
				pageBean.setCurrentPage(rows);//当前页面
				pageBean.setTotalPage(count);//总页数
				model.put("accountRecord", pageBean.getData());// // 当前页数据
				model.put("currentPage", pageBean.getCurrentPage()); // 当前页
				model.put("totalPage", pageBean.getTotalPage());//总页数
		/*	PageBean<RunfastAccountrecord> pageBean  = accountRecordService.getPageBeanbydriver(rows, page, accountRecord);
			model.put("accountRecord", pageBean.getData());
			model.put("currentPage", pageBean.getCurrentPage());
			model.put("totalPage", pageBean.getTotalPage());*/
			}
			
			return model;
			}
		
}
